package cn.xshi.sys.service.impl;
import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.sys.dao.SysEncoderQRCodeDao;
import cn.xshi.sys.form.SysEncoderQRCodeForm;
import cn.xshi.sys.model.SysEncoderQRCode;
import cn.xshi.sys.service.SysEncoderQRCodeService;
import cn.xshi.common.constant.SysContanst;
import cn.xshi.common.util.QR.QRUtil;
import org.springframework.stereotype.Service;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import javax.annotation.Resource;
/**
 * @Desc 平台二维码
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class SysEncoderQRCodeServiceImpl extends BaseService implements SysEncoderQRCodeService {

	@Resource
	private SysEncoderQRCodeDao sysEncoderqrcodeDao;

	@Resource
	private QRUtil qrUtil;

	/**
	* 分页
	* @param sysEncoderQRCodeForm
	* @return
	*/
	public List<SysEncoderQRCode> getSysEncoderQRCodeListByCondition(SysEncoderQRCodeForm sysEncoderQRCodeForm){
		try{
			List<SysEncoderQRCode> sysEncoderQRCodeList = sysEncoderqrcodeDao.getSysEncoderQRCodeListByCondition(sysEncoderQRCodeForm);
			if(CollectionUtil.isNotEmpty(sysEncoderQRCodeList)){
				for(SysEncoderQRCode sysEncoderQRCode : sysEncoderQRCodeList){
					String base64 = qrUtil.encodeImageBase64( "png", sysEncoderQRCode.getContent(), 430, 430, SysContanst.LOGO_BASE64);
					sysEncoderQRCode.setQrImage(base64);
				}
			}
			return sysEncoderQRCodeList;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public SysEncoderQRCode getSysEncoderQRCodeById(String id){
		try{
			SysEncoderQRCode sysEncoderQRCode = sysEncoderqrcodeDao.getSysEncoderQRCodeById(id);
			String base64 = qrUtil.encodeImageBase64( "png", sysEncoderQRCode.getContent(), 430, 430, SysContanst.LOGO_BASE64);
			sysEncoderQRCode.setQrImage(base64);
			return sysEncoderQRCode;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param sysEncoderQRCode
	* @return
	*/
	public int addSysEncoderQRCode(SysEncoderQRCode sysEncoderQRCode){
		int i = 0;
		try {
//			String base64 = qrUtil.encodeImageBase64( "png",SysEncoderQRCode.getContent(), 430, 430, SysContanst.LOGO_BASE64);
//			String newName = AllUtils.getRandom()+".png";
//			String path = getSysPathCache("xt_encoderqrcode_default_path").get(0).getXt_path();
//			String relative_path = getSysPathCache("xt_encoderqrcode_relative_path").get(0).getXt_path();
//			File f = new File(path);
//			if(!f.exists()){
//				f.mkdirs();
//			}
			sysEncoderQRCode.setCreateId(getCurrentAccountId());
			sysEncoderQRCode.setCreateTime(getDate());
////			twoDimensionCode.encoderQRCode(SysEncoderQRCode.getXt_encoderqrcode_url(), path+newName, "png");
//			XtAttachment xt_Attachment = new XtAttachment();
//			xt_Attachment.setXt_attachment_id(UUID.toUUID());
//			xt_Attachment.setXt_attachmentCtime(DateUtil.getSimpleDateFormat());
//			xt_Attachment.setXt_attachmentName(newName);
//			xt_Attachment.setXt_attachmentPath(relative_path+newName);
//			xt_Attachment.setXt_userinfo_id(getCurrentAccountId());
//			xt_Attachment.setXt_attachmentType("image/png");
//			xt_Attachment.setXt_attachmentTitle(SysEncoderQRCode.getXt_encoderqrcode_title());
//			xt_Attachment.setXt_userinfo_id(getCurrentAccountId());
//			xtAttachmentService.addXtAttachment(xt_Attachment);
//			SysEncoderQRCode.setXt_attachment_id(xt_Attachment.getXt_attachment_id());
			i = sysEncoderqrcodeDao.addSysEncoderQRCode(sysEncoderQRCode);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param sysEncoderQRCode
	* @return
	*/
	public int updateSysEncoderQRCode(SysEncoderQRCode sysEncoderQRCode){
		int i = 0;
		try {
			sysEncoderQRCode.setUpdateId(getCurrentAccountId());
			sysEncoderQRCode.setUpdateTime(getDate());
//			String newName = AllUtils.getRandom()+".png";
//			String path = getSysPathCache("xt_encoderqrcode_default_path").get(0).getXt_path();
//			String relative_path = getSysPathCache("xt_encoderqrcode_relative_path").get(0).getXt_path();
//			File f = new File(path);
//			if(!f.exists()){
//				f.mkdirs();
//			}
//			XtAttachment xt_Attachment = new XtAttachment();
//			xt_Attachment.setXt_attachment_id(UUID.toUUID());
//			xt_Attachment.setXt_attachmentCtime(getSimpleDateFormat());
//			xt_Attachment.setXt_attachmentName(newName);
//			xt_Attachment.setXt_attachmentPath(relative_path+newName);
//			xt_Attachment.setXt_userinfo_id(getCurrentAccountId());
//			xt_Attachment.setXt_attachmentType("image/png");
//			xt_Attachment.setXt_attachmentTitle(SysEncoderQRCode.getXt_encoderqrcode_title());
//			xt_Attachment.setXt_userinfo_id(getCurrentAccountId());
//			xtAttachmentService.addXtAttachment(xt_Attachment);
//			SysEncoderQRCode.setXt_attachment_id(xt_Attachment.getXt_attachment_id());
			i = sysEncoderqrcodeDao.updateSysEncoderQRCode(sysEncoderQRCode);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delSysEncoderQRCode(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysEncoderqrcodeDao.delSysEncoderQRCode(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}
}
